﻿//
//  Fansub Stat
//  Copyright (C) 2011 Victor Tereschenko (aka sibvic)
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//     
// ========================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using FansubStat;

namespace FansubStat.Subtitles
{
    class SrtParser
    {
        public static SubtitleData Parse(string fileName)
        {
            StringIterator lines = new StringIterator(System.IO.File.ReadAllLines(fileName, Encoding.GetEncoding(1251)));
            string line = lines.Get();

            Regex timeParser = new Regex(@"^(?<s_hour>\d+):(?<s_min>\d+):(?<s_sec>\d+).(?<s_msec>\d+) --> " +
                @"(?<e_hour>\d+):(?<e_min>\d+):(?<e_sec>\d+).(?<e_msec>\d+)$");

            SubtitleData subtitle = new SubtitleData();
            subtitle.FileName = System.IO.Path.GetFileName(fileName);
            while (line != null)
            {
                int lineID;
                if (int.TryParse(line, out lineID))
                {
                    line = lines.Get();
                    Match match = timeParser.Match(line);
                    if (match.Groups.Count == 9)
                    {
                        SubtitleDialog dialog = new SubtitleDialog();
                        dialog.StartTime = ParseTime(match, "s_hour", "s_min", "s_sec", "s_msec");
                        dialog.EndTime = ParseTime(match, "e_hour", "e_min", "e_sec", "e_msec");
                        line = lines.Get();
                        while (line != null && line.Trim().Length > 0)
                        {
                            if (dialog.Text != null && dialog.Text != "")
                                dialog.Text += "\n" + line;
                            else
                                dialog.Text = line;
                            line = lines.Get();
                        }
                        subtitle.Items.Add(dialog);
                    }
                }
                line = lines.Get();
            }

            return subtitle;
        }

        private static TimeSpan ParseTime(Match match, string hour, string min, string sec, string msec)
        {
            int start_hour = int.Parse(match.Groups[hour].Value);
            int start_min = int.Parse(match.Groups[min].Value);
            int start_sec = int.Parse(match.Groups[sec].Value);
            int start_msec = int.Parse(match.Groups[msec].Value);
            return new TimeSpan(0, start_hour, start_min, start_sec, start_msec);
        }
    }
}
